//
//  XueXiVC.swift
//  StudyChinese
//
//  Created by Wenzhe Fan on 2019/6/30.
//  Copyright © 2019年 Wenzhe Fan. All rights reserved.
//

import UIKit
import AVFoundation
enum StudyType:String{
    case 单字 = "单字"
    case 多字 = "多字"
    case 唐诗 = "唐诗"
    case 宋词 = "宋词"
    case 常用语 = "常用语"
    case 问候语 = "问候语"
    case 交通工具 = "交通工具"
}
class XueXiVC: UIViewController {
    
    var type:StudyType = .单字
    @IBOutlet weak var mytableView: UITableView!
    var hanziArr:[String] = ["衣","食","住","行"]
    
    var englishArr:[String] = ["food","food","food","food"]
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    func startRead(readstring:String = "波"){
        let avspeach = AVSpeechSynthesizer.init()
        let utterance = AVSpeechUtterance.init(string:readstring)
        utterance.rate = 0
        avspeach.speak(utterance)
    }

}
extension XueXiVC:UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.hanziArr.count
    }
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        switch self.type {
        case .单字:
             return self.hanziArr
        default:
            return []
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: "XueXiCell") as! XueXiCell
//
//        cell.hanziLB.text = hanziArr[indexPath.row]
//        return cell
        switch self.type {
        case .单字:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XueXiCell") as! XueXiCell
            cell.numLb.text =  "No.\(indexPath.row)"
            cell.hanziLB.text = hanziArr[indexPath.row]
            return cell
        case .多字:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XueXiCell2") as! XueXiCell2
            cell.numLb.text =  "No.\(indexPath.row)"
            cell.titleLB.text = hanziArr[indexPath.row]
            cell.readBack = { isChineseRead in
                if isChineseRead {
                  self.startRead(readstring: self.hanziArr[indexPath.row])
                }else{
                   self.startRead(readstring: self.englishArr[indexPath.row])
                }
            }
            return cell
        case .唐诗:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XueXiCell3") as! XueXiCell3
            cell.numLb.text =  "No.\(indexPath.row)"
            cell.titleLB.text = hanziArr[indexPath.row]
            cell.readBack = { isChineseRead in
                if isChineseRead {
                    self.startRead(readstring: self.hanziArr[indexPath.row])
                }else{
                    self.startRead(readstring: self.englishArr[indexPath.row])
                }
            }
            return cell
        case .宋词:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XueXiCell3") as! XueXiCell3
            cell.numLb.text =  "No.\(indexPath.row)"
            cell.titleLB.text = hanziArr[indexPath.row]
            cell.readBack = { isChineseRead in
                if isChineseRead {
                    self.startRead(readstring: self.hanziArr[indexPath.row])
                }else{
                    self.startRead(readstring: self.englishArr[indexPath.row])
                }
            }
            return cell
        case .交通工具:
            let cell = tableView.dequeueReusableCell(withIdentifier: "XueXiCell2") as! XueXiCell2
            cell.numLb.text =  "No.\(indexPath.row)"
            cell.titleLB.text = hanziArr[indexPath.row]
            cell.readBack = { isChineseRead in
                if isChineseRead {
                    self.startRead(readstring: self.hanziArr[indexPath.row])
                }else{
                    self.startRead(readstring: self.englishArr[indexPath.row])
                }
            }
            return cell
        case .常用语:
            break
        default:
            break
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "XueXiCell") as! XueXiCell
        cell.hanziLB.text = hanziArr[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch  type {
        case .单字:
            self.startRead(readstring: self.hanziArr[indexPath.row])
            break
        case .多字:
            break
        case .唐诗:
            break
        case .宋词:
            break
        case .常用语:
            break
        case .问候语:
            break
        case .交通工具:
            break
        }
    }
    
}
